<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品分类操作功能
 * 
 * @package 	htdocs
 * @subpackage  quanliadmin
 * @author  	create by allen.qiang
 */
class pa_goodsCategory {

    public function execute() {
		global $G_PGLOB ;
        if (isset($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //商品分类列表
                    $this->goodsCategoryList();
                break;
                case 2: //商品分类添加
                    $this->categoryAdd();
                break;
                case 3: //商品分类编辑
                    $this->categoryEdit();
                break;
                case 4: //中分类查看
                    $this->midCategoryView();
                break;
                case 5: //小分类查看
                    $this->smallCategoryView();
                break;
				default:
					$G_PGLOB->smarty->assign('error_msg', '参数错误，请审核后再操作');
					$G_PGLOB->smarty->display('comm/error.tpl');
            }
        }
    }

    /**
     * 商品分类列表
     * @author sunchun
     * 
     */
    private function goodsCategoryList() {
        global $G_PGLOB;
        
        $G_PGLOB->smarty->display('goods/category_list.tpl');
    }
    
    /**
     * 商品分类添加 
     *
     */
    private function categoryAdd() {
    	global $G_PGLOB;
    	
    	//获得分类标志，以便区分大、中、小分类
    	$cateFlg = isset($_REQUEST['cateFlg']) ? trim($_REQUEST['cateFlg']) : 1;
    	
    	//得到最大的分类排序
    	$categoryInfo = new entity('goods_category') ; 	
    	switch ($cateFlg){
    		case 1:	//大分类
    			$maxSortOrder = $G_PGLOB->DBCA->stmt_query('SELECT IF(MAX(sortOrder) IS NULL ,10 , MAX(sortOrder)+1) AS max_sortOrder FROM goods_bigcategory');
 				$categoryInfo->cateInfo = 1;	//大分类
    		break;
    		case 2:	//中分类
    			$maxSortOrder = $G_PGLOB->DBCA->stmt_query('SELECT IF(MAX(sortOrder) IS NULL ,100 , MAX(sortOrder)+1) AS max_sortOrder FROM goods_midcategory');
		    	
    			$categoryInfo->cateInfo = 2;	//中分类
    			
		    	//查询大分类信息
		    	$bigData = $G_PGLOB->DBCA->stmt_query('SELECT bigCategoryID,description FROM goods_bigcategory');
		    	$G_PGLOB->smarty->assign('bigData' , $bigData);
    		break;
    		case 3:	//小分类
    			$maxSortOrder = $G_PGLOB->DBCA->stmt_query('SELECT IF(MAX(sortOrder) IS NULL ,500 , MAX(sortOrder)+1) AS max_sortOrder FROM goods_smallcategory');
    			
    			$categoryInfo->cateInfo = 3;	//小分类
		    	
		    	//查询大分类信息
		    	$bigData = $G_PGLOB->DBCA->stmt_query('SELECT bigCategoryID,description FROM goods_bigcategory');
		    	$G_PGLOB->smarty->assign('bigData' , $bigData);

		    	$G_PGLOB->smarty->assign('smallCate' , $categoryInfo);
    		break;			
    	}
    	
    	//获取商品标签组
    	$tag_group = $G_PGLOB->DBCA->stmt_query('SELECT * FROM goods_tag_group WHERE useFlg = 1');
    	
    	//获得标签组对应分类
    	$category_tag = null;
    	//获取商品规格组
    	$spec_group = $G_PGLOB->DBCA->stmt_query('SELECT * FROM goods_spec_group WHERE useFlg = 1');
    	//获取已选规格组
		$category_spec = null;
		$categoryInfo->cateType  = 1;	//添加
		$categoryInfo->useFlg    = 1 ;	//状态
		$categoryInfo->sortOrder = $maxSortOrder[0]->max_sortOrder ;	//排序
    	$G_PGLOB->smarty->assign('categoryInfo' , $categoryInfo);
    	$G_PGLOB->smarty->assign('tag_group' , $tag_group);
    	$G_PGLOB->smarty->assign('category_tag' , $category_tag);
    	$G_PGLOB->smarty->assign('spec_group',$spec_group);
    	$G_PGLOB->smarty->assign('category_spec',$category_spec);
    	$G_PGLOB->smarty->display('goods/category_edit.tpl');
    }
    
    /**
     * 商品分类编辑 
     *
     */
    private function categoryEdit() {
    	global $G_PGLOB;
    	
    	//接收参数
    	$cateStr = isset($_REQUEST['categoryID']) ? trim($_REQUEST['categoryID']) : null;
    	if(empty($cateStr)){
    		$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
    		die();
    	}
    	
    	//把参数拆成数组
    	$cateArr = explode('_', $cateStr);
    	switch (count($cateArr)){
    		case 1:	//大分类
    			$categoryInfo = new entity('goods_bigcategory') ; 
    			$categoryInfo->bigCategoryID = $cateArr[0];
    			if(!$G_PGLOB->DBCA->fetch($categoryInfo)){
    				$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
		    		die();
    			}else{
    				$categoryInfo->cateType = 2;	//修改
    				$categoryInfo->cateInfo = 1;    //大分类
    			}
    			
    			//标签组对应分类ID
    			$categoryID = $cateArr[0];
    		break;
    		case 2: //中分类
    			$categoryInfo = new entity('goods_midcategory') ; 
    			$categoryInfo->bigCategoryID = $cateArr[0];
    			$categoryInfo->midCategoryID = $cateArr[1];
    			if(!$G_PGLOB->DBCA->fetch($categoryInfo)){
    				$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
		    		die();
    			}else{
    				$categoryInfo->cateType = 2;	//修改
    				$categoryInfo->cateInfo = 2;    //中分类
    			}
    			
    			//查询大分类信息
		    	$bigData = $G_PGLOB->DBCA->stmt_query('SELECT bigCategoryID,description FROM goods_bigcategory');
		    	$G_PGLOB->smarty->assign('bigData' , $bigData);
		    	
		    	//标签组对应分类ID
    			$categoryID = $cateArr[0] . $cateArr[1];
    		break;
    		case 3: //小分类
    			$categoryInfo = new entity('goods_smallcategory') ; 
    			$categoryInfo->bigCategoryID   = $cateArr[0];
    			$categoryInfo->midCategoryID   = $cateArr[1];
    			$categoryInfo->smallCategoryID = $cateArr[2];
    			if(!$G_PGLOB->DBCA->fetch($categoryInfo)){
    				$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
		    		die();
    			}else{
    				$categoryInfo->cateType = 2;	//修改
    				$categoryInfo->cateInfo = 3;    //小分类
    			}
    			
    			//查询大分类信息
		    	$bigData = $G_PGLOB->DBCA->stmt_query('SELECT bigCategoryID,description FROM goods_bigcategory');
		    	$G_PGLOB->smarty->assign_by_ref('bigData' , $bigData);
		    	
		    	//根据大分类查询中分类信息
		    	$midData = $G_PGLOB->DBCA->stmt_query(sprintf('SELECT midCategoryID,description FROM goods_midcategory WHERE bigCategoryID = %s', $cateArr[0]));
		    	$G_PGLOB->smarty->assign_by_ref('midData' , $midData);
		    	
		    	//标签组对应分类ID
    			$categoryID = $cateArr[0] . $cateArr[1] . $cateArr[2];
    		break;	
    	}
    	
    	//获取商品标签组
    	$tag_group = $G_PGLOB->DBCA->stmt_query('SELECT * FROM goods_tag_group WHERE useFlg = 1');
    	
    	//获取已选标签组
    	$category_tag = $G_PGLOB->DBCA->stmt_query(sprintf('SELECT * FROM goods_category_tagGroup WHERE categoryID = %s', $categoryID));
    	
    	//获取商品规格组
    	$spec_group = $G_PGLOB->DBCA->stmt_query('SELECT * FROM goods_spec_group WHERE useFlg = 1');
    	//获取已选规格组
		$category_spec = $G_PGLOB->DBCA->stmt_query(sprintf('SELECT * FROM goods_category_specGroup WHERE categoryID = %s',$categoryID));
    	$G_PGLOB->smarty->assign('categoryInfo' , $categoryInfo);
    	$G_PGLOB->smarty->assign('tag_group' , $tag_group);
    	$G_PGLOB->smarty->assign('category_tag' , $category_tag);
    	$G_PGLOB->smarty->assign('spec_group',$spec_group);
    	$G_PGLOB->smarty->assign('category_spec',$category_spec);
    	$G_PGLOB->smarty->display('goods/category_edit.tpl');
    } 
    
    /**
     * 中分类查看
     *
     */
    private function midCategoryView(){
    	global $G_PGLOB;
    	
    	$bigCategoryID = isset($_REQUEST['bigCategoryID']) ? trim($_REQUEST['bigCategoryID']) : null;
    	if(empty($bigCategoryID)){
    		$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
    		die();
    	}
    	
    	$bigCategory = new entity('goods_bigcategory');
    	$bigCategory->bigCategoryID = $bigCategoryID;
    	if($G_PGLOB->DBCA->fetch($bigCategory))
    		$cateTitle = $bigCategory->description;
    	else 
    		$cateTitle = '';	
    	
    	$bigData = $G_PGLOB->DBCA->stmt_query(sprintf('SELECT * FROM goods_midcategory WHERE bigCategoryID = %s ORDER BY midCategoryID DESC', $bigCategoryID));
    	
    	$G_PGLOB->smarty->assign('cateTitle', $cateTitle);
    	$G_PGLOB->smarty->assign_by_ref('bigData', $bigData);
    	$G_PGLOB->smarty->display('goods/midCategory_view.tpl');
    }
    
    /**
     * 小分类查看
     *
     */
    private function smallCategoryView(){
    	global $G_PGLOB;
    	
    	$midCategoryID = isset($_REQUEST['midCategoryID']) ? trim($_REQUEST['midCategoryID']) : null;

    	if(empty($midCategoryID)){
    		$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
    		die();
    	}
    	
    	$categoryArray = explode('_', $midCategoryID);
    	
    	$midCategory = new entity('goods_midcategory');
    	$midCategory->bigCategoryID = $categoryArray[0];
    	$midCategory->midCategoryID = $categoryArray[1];
    	if($G_PGLOB->DBCA->fetch($midCategory))
    		$cateTitle = $midCategory->description;
    	else 
    		$cateTitle = '';	
    	
    	$smallData = $G_PGLOB->DBCA->stmt_query(sprintf('SELECT * FROM goods_smallcategory WHERE bigCategoryID = %s AND midCategoryID = %s ORDER BY smallCategoryID DESC', $categoryArray[0], $categoryArray[1]));

    	$G_PGLOB->smarty->assign('cateTitle', $cateTitle);
    	$G_PGLOB->smarty->assign_by_ref('smallData', $smallData);
    	$G_PGLOB->smarty->display('goods/smallCategory_view.tpl');
    }
    
}